package com.wawei.seeds.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wawei.seeds.R;
import com.wawei.seeds.adapter.WalnutServiceDetailItemActivityAdapter;
import com.wawei.seeds.champion.ChampionCommentActivity;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.services.WalnutMainServiceHandler;
import com.wawei.seeds.services.WalnutMyServiceServiceHandler;
import com.wawei.seeds.utils.ImageLoader;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;

import java.io.Serializable;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 * Created by t27891 on 15/6/28.
 */
public class MeServiceDetailActivity extends ActionBarActivity {
    public static final String ITEM_DATA = "item";
    @InjectView(R.id.walnut_logo)ImageView logoImage;
    @InjectView(R.id.walnut_event_name)TextView eventName;
    @InjectView(R.id.walnut_event_image)ImageView eventImage;
    @InjectView(R.id.walnut_available_amount)TextView availableAmount;
    @InjectView(R.id.walnut_event_title)TextView eventTitle;
    @InjectView(R.id.walnut_event_action)TextView eventAction;

    @InjectView(R.id.walnut_event_detail)TextView eventDetail;
    @InjectView(R.id.walnut_event_sharing)View eventSharing;

    @InjectView(R.id.walnut_event_value)TextView eventValue;
    @InjectView(R.id.walnut_event_confirmed)TextView eventConfirmed;
    @InjectView(R.id.walnut_event_book)TextView eventBook;

    @InjectView(R.id.my_event_appointment_list)ListView appointmentList;

    @InjectView(R.id.walnut_comment_service)View comment;

    private WalnutServiceDetailItemActivityAdapter detailItemActivityAdapter;
    private WalnutMainEvent event;
    private ServiceDetailNotifyHandler notifyHandler;

    private ImageLoader imageLoader;

    private static final int GET_DETAIL_BOOKING = 0x01;
    private static final int GET_EVENT_INFO = 0x02;
    public static final int ACCEPT_BOOKING = 0x03;
    public static final int CANCEL_BOOKING = 0x04;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walnut_me_service_detail);
        ButterKnife.inject(this);

        Serializable data = (Serializable)getIntent().getSerializableExtra(ITEM_DATA);
        if(null == data){
            Toast.makeText(this, R.string.exception_no_data, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        event = (WalnutMainEvent)data;
        detailItemActivityAdapter = new WalnutServiceDetailItemActivityAdapter(this);
        notifyHandler = new ServiceDetailNotifyHandler(this);

        imageLoader = new ImageLoader(this);

        init();
        initList();
    }

    private void initList(){
        WalnutMyServiceServiceHandler serviceServiceHandler = new WalnutMyServiceServiceHandler(this);
        serviceServiceHandler.retrieveAppointmentManList(RetrofitNetworkService.makeAsyncRestAdapter(),
                String.valueOf(event.eventId), notifyHandler, GET_DETAIL_BOOKING);
    }

    private void init(){
        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (event != null && event.publisherId != null) {
                    Intent intent = new Intent(MeServiceDetailActivity.this, ChampionCommentActivity.class);
                    intent.putExtra("providerId", event.publisherId);
                    MeServiceDetailActivity.this.startActivity(intent);
                } else {
                    Toast.makeText(MeServiceDetailActivity.this, "错误的发布者数据！", Toast.LENGTH_LONG).show();
                }
            }
        });

        logoImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                finish();
            }
        });
        eventSharing.setVisibility(View.INVISIBLE);
        eventName.setText(R.string.walnut_event_detail_title);
        if(null != event){
            initEvent();
        }

        WalnutMainServiceHandler mainServiceHandler = new WalnutMainServiceHandler(this);
        mainServiceHandler.retrieveEventDetail(RetrofitNetworkService.makeAsyncRestAdapter(),
                event.eventId, notifyHandler, GET_EVENT_INFO);
    }

    private void initEvent(){
        availableAmount.setText(event.available);
        eventTitle.setText(event.title);
        eventAction.setText(event.action);
        eventDetail.setText(event.detail);
        eventValue.setText(event.value);
        eventBook.setText(event.booked);
        eventConfirmed.setText(event.confirmed);
        imageLoader.loadImage(event.AD, eventImage, false, false);
    }

    public static class ServiceDetailNotifyHandler extends WalnutBaseNofityHandler<MeServiceDetailActivity, WalnutMainEvent> {
        public ServiceDetailNotifyHandler(MeServiceDetailActivity meServiceDetailActivity) {
            super(meServiceDetailActivity);
        }

        @Override
        public void handleOtherMessage(Message msg) {
            Bundle bundle = msg.getData();
            if(msg.what == GET_DETAIL_BOOKING){
                WalnutMainEvent remoteEvent = (WalnutMainEvent)bundle.get(RetrofitNetworkService.RESULT_DATA);
                if (remoteEvent != null && remoteEvent.bookerList != null) {
                    get().detailItemActivityAdapter.setDataset(remoteEvent.bookerList);
                    get().appointmentList.setAdapter(get().detailItemActivityAdapter);
                }
            }
            else if(msg.what == GET_EVENT_INFO){
                WalnutMainEvent remoteEvent = (WalnutMainEvent)bundle.get(RetrofitNetworkService.RESULT_DATA);
                if (remoteEvent != null) {
                    get().event = remoteEvent;
                    get().initEvent();
                }
            } else if (msg.what == ACCEPT_BOOKING) {
                String bookingId = (String)bundle.get(RetrofitNetworkService.RESULT_DATA);

                if (bookingId != null) {
                    get().detailItemActivityAdapter.notifyDataSetChanged(bookingId, "accepted");
                    Toast.makeText(get(), "确认成功!", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(get(), "确认失败!", Toast.LENGTH_LONG).show();
                }
            } else if (msg.what == CANCEL_BOOKING) {
                String bookingId = (String)bundle.get(RetrofitNetworkService.RESULT_DATA);

                if (bookingId != null) {
                    get().detailItemActivityAdapter.notifyDataSetChanged(bookingId, "cancel");
                    Toast.makeText(get(), "取消成功!", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(get(), "取消失败!", Toast.LENGTH_LONG).show();
                }
            }
            super.handleOtherMessage(msg);
        }
    }
}
